﻿// ================================================================================
// 		File: PatyEduSpecial.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（--）
// 		Date: 2015-12-27 22:18
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// ================================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using MySql.Data.MySqlClient;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Org
{
    public class PartyOrgBranch
    {
        #region 字段属性
        [Key]
        public string BranchID { get; set; }

        /// <summary>
        /// 组织名
        /// </summary>
        public string BranchName { get; set; }

        /// <summary>
        /// 父级
        /// </summary>
        public string ParentBranchID { get; set; }

        /// <summary>
        /// 组织类型 1国有集体企业,2机关单位,3“两新”组织,4村改制公司,5事业单位,6社区
        /// </summary>
        public string BranchType { get; set; }

        /// <summary>
        /// 组织标志 1、顶层党委 ，2、党委、党工委 ， 4、总支、二级党委， 3、普通支部，5、第四级支部 
        /// </summary>]
        public string BranchMark { get; set; }

        /// <summary>
        /// OrderNo
        /// </summary>

        public int OrderNo { get; set; }

        /// <summary>
        /// 描术
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// logo
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// 联系人
        /// </summary>
        public string LinkMan { get; set; }

        /// <summary>
        /// 联系人电话
        /// </summary>
        public string LinkPhone { get; set; }

        /// <summary>
        /// “两新”类型
        /// </summary>
        public int SocialParty { get; set; }
        /// <summary>
        /// 
        /// </summary>

        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 
        /// </summary>

        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 经度
        /// </summary>
        public string XPoint { get; set; }

        /// <summary>
        /// 纬度
        /// </summary>
        public string YPoint { get; set; }


        /// <summary>
        /// 菜单在树形结构中的层级（从0开始）
        /// </summary>
        [NotMapped]
        public int TreeLevel { get; set; }

        /// <summary>
        /// 是否可用（默认true）,在模拟树的下拉列表中使用
        /// </summary>
        [NotMapped]
        public bool Enabled { get; set; }

        /// <summary>
        /// 是否叶子节点（默认true）
        /// </summary>
        [NotMapped]
        public bool IsTreeLeaf { get; set; }

        public object Clone()
        {
            PartyOrgBranch OrgBranch = new PartyOrgBranch
            {
                Address = Address,
                BranchMark = BranchMark,
                BranchID = BranchID,
                BranchName = BranchName,
                TreeLevel = TreeLevel,
                Enabled = Enabled,
                IsTreeLeaf = IsTreeLeaf,
                BranchType = BranchType,
                CreateTime = CreateTime,
                Description = Description,
                ImageUrl = ImageUrl,
                LinkMan = LinkMan,
                LinkPhone = LinkPhone,
                OrderNo = OrderNo,
                ModifyTime = ModifyTime
            };
            return OrgBranch;
        }
        #endregion

        #region 数据查询

        /// <summary>
        /// 根据父ID获取组织
        /// </summary>
        /// <returns></returns>
        public static DataSet GetBranchByParentId(string parentId, string key, int pageIndex = 0, int pageSize = 20)
        {
            var para = new List<MySqlParameter>
            {
                new MySqlParameter(){DbType = DbType.String,ParameterName = "ParentBranchID",Value = parentId},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "DDicType",Value = SysDataDictionary.BranchTypeKey},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "DDicTypee",Value = SysDataDictionary.BranchMarkKey}
            };
            var sql = new StringBuilder(@"SELECT SQL_CALC_FOUND_ROWS d.*,
                            dic.DDicMemberName  BranchTypeName,dice.DDicMemberName  BranchMarkName
                            FROM tblpartyorgbranch d
                            
                            left join tbldatadictionary dic on (dic.DDicType=?DDicType  and dic.DDicMember=d.BranchType )
                            left join tbldatadictionary dice on (dice.DDicType=?DDicTypee and dice.DDicMember=d.BranchMark)
                            WHERE ParentBranchID=?ParentBranchID         
");

            if (!string.IsNullOrEmpty(key))
            {
                sql.AppendFormat(" and d.BranchName like ?key ");
                para.Add(new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "key",
                    Value = string.Format("%{0}%", key)
                });
            }
            sql.Append(" ORDER BY d.OrderNo desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sql.ToString(), para.ToArray());
        }

        /// <summary>
        /// 获取组织
        /// </summary>
        /// <returns></returns>
        public static DataSet GetBranchAll()
        {
            var sql = new StringBuilder(@"SELECT  * FROM tblpartyorgbranch  order by orderNo,BranchName");
            return MySqlHelper.ExecuteDataSet(sql.ToString());
        }

        public static DataSet GetCameraBranchAll()
        {
            var sql = new StringBuilder(@"select * from (SELECT  BranchID,ParentBranchID,BranchName,0 as BranchType FROM tblpartyorgbranch order by OrderNo,BranchName)t
UNION
select ID,BranchID,CameraName,1 from tblpartyvideoprobe");
            return MySqlHelper.ExecuteDataSet(sql.ToString());
        }

        /// <summary>
        /// 根据ID获取组织
        /// </summary>
        /// <returns></returns>
        public static DataTable GetBranchBId(string Id)
        {
            var para = new List<MySqlParameter>
            {
                new MySqlParameter(){DbType = DbType.String,ParameterName = "BranchID",Value = Id},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "DDicType1",Value = SysDataDictionary.BranchTypeKey},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "DDicType2",Value = SysDataDictionary.BranchMarkKey}
            };
            var sql = new StringBuilder(@"SELECT  d.*,dd.BranchName ParentBranchIName,
                            dic.DDicMemberName as BranchTypeName,dic2.DDicMemberName as BranchMarkName
                            
                            FROM tblpartyorgbranch d
                            left join tblpartyorgbranch dd on (dd.branchID=d.ParentBranchID)
                            left join tbldatadictionary dic on (dic.DDicType=?DDicType1 and dic.DDicMember=d.BranchType)
                            left join tbldatadictionary dic2 on (dic2.DDicType=?DDicType2 and dic2.DDicMember=d.BranchMark)    
                            WHERE d.BranchID=?BranchID ;                       
                            ");

            return MySqlHelper.ExecuteDataSet(sql.ToString(), para.ToArray()).Tables[0];
        }

        public static bool IsHasChildrenBranch(string branchId)
        {
            var para = new List<MySqlParameter>
            {
                new MySqlParameter(){DbType = DbType.String,ParameterName = "BranchID",Value = branchId}
            };
            var sql = new StringBuilder(@"SELECT COUNT(*) FROM tblpartyorgbranch WHERE ParentBranchID=?BranchID;");

            return Convert.ToInt32(MySqlHelper.ExecuteScalar(sql.ToString(), para.ToArray())) > 0;
        }

        public static bool IsOfficialPartyBranch(string branchId)
        {
            var para = new List<MySqlParameter>
            {
                new MySqlParameter(){DbType = DbType.String,ParameterName = "BranchID",Value = branchId}
            };
            var sql = new StringBuilder(@"SELECT COUNT(*) FROM tblpartyorgbranch WHERE ParentBranchID='63d9da4166204a7b8351e26812e93672' and branchid=?BranchID;");

            return Convert.ToInt32(MySqlHelper.ExecuteScalar(sql.ToString(), para.ToArray())) > 0;
        }

        public static bool IsStreetOrViliage(string branchId)
        {
            var boolResult = false;
            var para = new List<MySqlParameter>
            {
                new MySqlParameter(){DbType = DbType.String,ParameterName = "BranchID",Value = branchId}
            };
            var sql = new StringBuilder(@"SELECT BranchType FROM tblpartyorgbranch WHERE BranchID=?BranchID;");

            var result = MySqlHelper.ExecuteScalar(sql.ToString(), para.ToArray());
            if (result != null)
            {
                if (result.ToString() == "7" || result.ToString() == "8") {
                    boolResult = true;
                }
            }

            return boolResult;
        }
        #endregion
    }
}
